<?php

namespace Admin\Controller\Analysis;

use Helper\AnalysisApi;
use Helper\ElasticQuery;

/**
 * Class AnalysisCountryController
 * @package Admin\Controller
 */
class AnalysisOfferDetailController extends AnalysisController
{
	protected $_tpl_index = 'Analysis/CountryIndex';
	protected $_tpl_search = 'Analysis/OfferDetail';
	protected $_priority = "";

	function _before_search()
	{
	    $this->assign("country", $this->geo());
	}

    function _before_searchdata(array $post)
    {
        if (!empty($post['priority']))
        {
            $this->_priority = $post['priority'];
        }
        //禁止跨索引查询数据
        if(date("Ym", strtotime($post["end_day"])) - date("Ym", strtotime($post["start_day"])) > 1)
        {
            $this->error("不能跨月查询数据");
            return false;
        }

        if(strtotime($post["end_day"]) - strtotime($post["start_day"]) > 86400 * 7)
        {
            if(empty($post["offer_id"]) && empty($post["country_id"]))

                $this->error("不能查询超过一周的数据");
            return false;
        }
    }

	function searchdata(array $search)
	{
        $list = $eventlogs = $postback = [];
        if ($this->_priority == 'eventlogs')
        {
            if (!$search['rob'])
            {
                $eventlogs = $this->getEventlogs($search);
                $offerIds = array_column($eventlogs, 'offer_id');
            }
            if (!empty($offerIds))
            {
                $search['offer_id'] = $offerIds;
                $list = $this->getPostback($search);
            } else {
                $list = $this->getPostback($search);
            }

            foreach ($list as $k => $val)
            {
                $list[$k] = array_merge($val, (array)$eventlogs[$k]);
            }
        } elseif($this->_priority == 'postback') {
            $list = $this->getPostback($search);
            if (!$search['rob'])
            {
                $offerIds = array_column($list, 'offer_id');
                if (!empty($offerIds))
                {
                    $search['offer_id'] = $offerIds;
                    $eventlogs = $this->getEventlogs($search);
                }
            }
            foreach ($list as $k => $val)
            {
                $list[$k] = array_merge($val, (array)$eventlogs[$k]);
            }
        } else {
            $list = $this->getPostback($search);
            if (!$search['rob'])
            {
                $eventlogs = $this->getEventlogs($search);
            }

            if (!empty($eventlogs))
            {
                foreach ($eventlogs as $k => $val)
                {
                    if (!empty($list[$k]))
                    {
                        $list[$k] = array_merge($val, (array)$list[$k]);
                    } else {
                        $list[$k] = $val;
                    }
                }
            }
        }

		return $list;
	}

    /**
     * eventlogs
     * @param array $search
     * @return array
     */
	protected function getEventlogs(array $search)
    {
        $list = [];
        $search['index'] = "eventlogs";
        $search['source'] = 'show,show_user,click,click_user,installed,installed_user';
        $search['aggs'] = 'offer_id';
        $search['mark'] = CONTROLLER_NAME . "-tian";
        try {
            $eventlogs = AnalysisApi::getResponse($search);
        } catch (\Exception $e) {
            $this->error(APP_DEBUG ? $e->getMessage() : "系统错误，请通知管理员");
        }

        if (!empty($eventlogs))
        {
            foreach ($eventlogs as $val)
            {
                $list[$val['offer_id']]['offer_id'] = $val['offer_id'];
                $list[$val['offer_id']]['show'] += intval($val['show']);
                $list[$val['offer_id']]['show_user'] += intval($val['show_user']);
                $list[$val['offer_id']]['click'] += intval($val['click']);
                $list[$val['offer_id']]['click_user'] += intval($val['click_user']);
                $list[$val['offer_id']]['installed'] += intval($val['installed']);
                $list[$val['offer_id']]['installed_user'] += intval($val['installed_user']);
            }
        }
        return $list;
    }

    /**
     * postback
     * @param array $search
     * @return array
     */
    protected function getPostback(array $search)
    {
        $list = [];
        //收益
        $search['index'] = "postback";
        $search['source'] = 'conversion,conversion_user,income';
        $search['aggs'] = 'offer_id';
        $search['mark'] = CONTROLLER_NAME . "-tian";
        try {
            $postback = AnalysisApi::getResponse($search);
        } catch (\Exception $e) {
            $this->error(APP_DEBUG ? $e->getMessage() : "系统错误，请通知管理员");
        }

        if (!empty($postback))
        {
            foreach ($postback as $k => $v)
            {
                $list[$v['offer_id']]['offer_id'] = $v['offer_id'];
                $list[$v['offer_id']]['conversion'] += intval($v['conversion']);
                $list[$v['offer_id']]['conversion_user'] += intval($v['conversion_user']);
                $list[$v['offer_id']]['income'] += intval($v['income']);
            }
        }
        return $list;
    }

	function geo()
	{
	    $geo = <<<EEE
		[{"id":"32","name":"Argentina","code":"AR"},{"id":"36","name":"Australia","code":"AU"},{"id":"40","name":"Austria","code":"AT"},{"id":"48","name":"Bahrain","code":"BH"},{"id":"76","name":"Brazil","code":"BR"},{"id":"152","name":"Chile","code":"CL"},{"id":"170","name":"Colombia","code":"CO"},{"id":"203","name":"Czech Republic","code":"CZ"},{"id":"218","name":"Ecuador","code":"EC"},{"id":"246","name":"Finland","code":"FI"},{"id":"250","name":"France","code":"FR"},{"id":"276","name":"Germany","code":"DE"},{"id":"288","name":"Ghana","code":"GH"},{"id":"348","name":"Hungary","code":"HU"},{"id":"356","name":"India","code":"IN"},{"id":"360","name":"Indonesia","code":"ID"},{"id":"372","name":"Ireland","code":"IE"},{"id":"392","name":"Japan","code":"JP"},{"id":"400","name":"Jordan","code":"JO"},{"id":"404","name":"Kenya","code":"KE"},{"id":"442","name":"Luxembourg","code":"LU"},{"id":"458","name":"Malaysia","code":"MY"},{"id":"484","name":"Mexico","code":"MX"},{"id":"528","name":"Netherlands","code":"NL"},{"id":"566","name":"Nigeria","code":"NG"},{"id":"578","name":"Norway","code":"NO"},{"id":"591","name":"Panama","code":"PA"},{"id":"608","name":"Philippines","code":"PH"},{"id":"616","name":"Poland","code":"PL"},{"id":"620","name":"Portugal","code":"PT"},{"id":"642","name":"Romania","code":"RO"},{"id":"682","name":"Saudi Arabia","code":"SA"},{"id":"702","name":"Singapore","code":"SG"},{"id":"704","name":"Viet nam","code":"VN"},{"id":"705","name":"Slovenia","code":"SI"},{"id":"710","name":"South Africa","code":"ZA"},{"id":"724","name":"Spain","code":"ES"},{"id":"756","name":"Switzerland","code":"CH"},{"id":"764","name":"Thailand","code":"TH"},{"id":"784","name":"United Arab Emirates","code":"AE"},{"id":"807","name":"Macedonia - The Frm Yugoslav Rep Of","code":"MK"},{"id":"818","name":"Egypt","code":"EG"},{"id":"819","name":"United States","code":"US"},{"id":"1567","name":"Andorra","code":"AD"},{"id":"1568","name":"Afghanistan","code":"AF"},{"id":"1569","name":"Antigua and Barbuda","code":"AG"},{"id":"1570","name":"Anguilla","code":"AI"},{"id":"1571","name":"Albania","code":"AL"},{"id":"1572","name":"Armenia","code":"AM"},{"id":"1573","name":"Angola","code":"AO"},{"id":"1574","name":"Antarctica","code":"AQ"},{"id":"1575","name":"American Samoa","code":"AS"},{"id":"1576","name":"Aruba","code":"AW"},{"id":"1577","name":"Aland Islands","code":"AX"},{"id":"1578","name":"Azerbaijan","code":"AZ"},{"id":"1579","name":"Bosnia and Herzegovina","code":"BA"},{"id":"1580","name":"Barbados","code":"BB"},{"id":"1581","name":"Bangladesh","code":"BD"},{"id":"1582","name":"Belgium","code":"BE"},{"id":"1583","name":"Burkina Faso","code":"BF"},{"id":"1584","name":"Bulgaria","code":"BG"},{"id":"1585","name":"Burundi","code":"BI"},{"id":"1586","name":"Benin","code":"BJ"},{"id":"1587","name":"Saint Barthelemy","code":"BL"},{"id":"1588","name":"Bermuda","code":"BM"},{"id":"1589","name":"Brunei Darussalam","code":"BN"},{"id":"1590","name":"Bolivia, Plurinational State of","code":"BO"},{"id":"1591","name":"Bonaire, Sint Eustatius and Saba","code":"BQ"},{"id":"1592","name":"Bahamas","code":"BS"},{"id":"1593","name":"Bhutan","code":"BT"},{"id":"1594","name":"Bouvet Island","code":"BV"},{"id":"1595","name":"Botswana","code":"BW"},{"id":"1596","name":"Belarus","code":"BY"},{"id":"1597","name":"Belize","code":"BZ"},{"id":"1598","name":"Canada","code":"CA"},{"id":"1600","name":"Congo, the Democratic Republic of the","code":"CD"},{"id":"1601","name":"Central African Republic","code":"CF"},{"id":"1602","name":"Congo","code":"CG"},{"id":"1603","name":"Cote d'Ivoire !C?te d'Ivoire","code":"CI"},{"id":"1604","name":"Cook Islands","code":"CK"},{"id":"1605","name":"Cameroon","code":"CM"},{"id":"1606","name":"China","code":"CN"},{"id":"1607","name":"Costa Rica","code":"CR"},{"id":"1608","name":"Cuba","code":"CU"},{"id":"1609","name":"Cape Verde","code":"CV"},{"id":"1610","name":"Cura?ao","code":"CW"},{"id":"1611","name":"Christmas Island","code":"CX"},{"id":"1612","name":"Cyprus","code":"CY"},{"id":"1613","name":"Djibouti","code":"DJ"},{"id":"1614","name":"Denmark","code":"DK"},{"id":"1615","name":"Dominica","code":"DM"},{"id":"1616","name":"Dominican Republic","code":"DO"},{"id":"1617","name":"Algeria","code":"DZ"},{"id":"1618","name":"Estonia","code":"EE"},{"id":"1619","name":"Western Sahara","code":"EH"},{"id":"1620","name":"Eritrea","code":"ER"},{"id":"1621","name":"Ethiopia","code":"ET"},{"id":"1622","name":"Fiji","code":"FJ"},{"id":"1623","name":"Falkland Islands (Malvinas)","code":"FK"},{"id":"1624","name":"Micronesia, Federated States of","code":"FM"},{"id":"1625","name":"Faroe Islands","code":"FO"},{"id":"1626","name":"Gabon","code":"GA"},{"id":"1627","name":"United Kingdom","code":"GB"},{"id":"1628","name":"Grenada","code":"GD"},{"id":"1629","name":"Georgia","code":"GE"},{"id":"1630","name":"French Guiana","code":"GF"},{"id":"1631","name":"Guernsey","code":"GG"},{"id":"1632","name":"Gibraltar","code":"GI"},{"id":"1633","name":"Greenland","code":"GL"},{"id":"1634","name":"Gambia","code":"GM"},{"id":"1635","name":"Guinea","code":"GN"},{"id":"1636","name":"Guadeloupe","code":"GP"},{"id":"1637","name":"Equatorial Guinea","code":"GQ"},{"id":"1638","name":"Greece","code":"GR"},{"id":"1639","name":"South Georgia and the South Sandwich Islands","code":"GS"},{"id":"1640","name":"Guatemala","code":"GT"},{"id":"1641","name":"Guam","code":"GU"},{"id":"1642","name":"Guinea-Bissau","code":"GW"},{"id":"1643","name":"Guyana","code":"GY"},{"id":"1644","name":"Hong Kong","code":"HK"},{"id":"1646","name":"Honduras","code":"HN"},{"id":"1647","name":"Croatia","code":"HR"},{"id":"1648","name":"Haiti","code":"HT"},{"id":"1649","name":"Israel","code":"IL"},{"id":"1650","name":"Isle of Man","code":"IM"},{"id":"1651","name":"British Indian Ocean Territory","code":"IO"},{"id":"1652","name":"Iraq","code":"IQ"},{"id":"1653","name":"Iran, Islamic Republic of","code":"IR"},{"id":"1654","name":"Iceland","code":"IS"},{"id":"1655","name":"Italy","code":"IT"},{"id":"1656","name":"Jersey","code":"JE"},{"id":"1657","name":"Jamaica","code":"JM"},{"id":"1658","name":"Kyrgyzstan","code":"KG"},{"id":"1659","name":"Cambodia","code":"KH"},{"id":"1660","name":"Kiribati","code":"KI"},{"id":"1661","name":"Comoros","code":"KM"},{"id":"1662","name":"Saint Kitts and Nevis","code":"KN"},{"id":"1663","name":"Korea, Democratic People's Republic of","code":"KP"},{"id":"1664","name":"Korea, Republic of","code":"KR"},{"id":"1665","name":"Kuwait","code":"KW"},{"id":"1666","name":"Cayman Islands","code":"KY"},{"id":"1667","name":"Kazakhstan","code":"KZ"},{"id":"1668","name":"Lao People's Democratic Republic","code":"LA"},{"id":"1669","name":"Lebanon","code":"LB"},{"id":"1670","name":"Saint Lucia","code":"LC"},{"id":"1671","name":"Liechtenstein","code":"LI"},{"id":"1672","name":"Sri Lanka","code":"LK"},{"id":"1673","name":"Liberia","code":"LR"},{"id":"1674","name":"Lesotho","code":"LS"},{"id":"1675","name":"Lithuania","code":"LT"},{"id":"1676","name":"Latvia","code":"LV"},{"id":"1677","name":"Libya","code":"LY"},{"id":"1678","name":"Morocco","code":"MA"},{"id":"1679","name":"Monaco","code":"MC"},{"id":"1680","name":"Moldova, Republic of","code":"MD"},{"id":"1681","name":"Montenegro","code":"ME"},{"id":"1682","name":"Saint Martin (French part)","code":"MF"},{"id":"1683","name":"Madagascar","code":"MG"},{"id":"1684","name":"Marshall Islands","code":"MH"},{"id":"1685","name":"Mali","code":"ML"},{"id":"1686","name":"Myanmar","code":"MM"},{"id":"1687","name":"Mongolia","code":"MN"},{"id":"1688","name":"Macao","code":"MO"},{"id":"1689","name":"Northern Mariana Islands","code":"MP"},{"id":"1690","name":"Martinique","code":"MQ"},{"id":"1691","name":"Mauritania","code":"MR"},{"id":"1692","name":"Montserrat","code":"MS"},{"id":"1693","name":"Malta","code":"MT"},{"id":"1694","name":"Mauritius","code":"MU"},{"id":"1695","name":"Maldives","code":"MV"},{"id":"1696","name":"Malawi","code":"MW"},{"id":"1697","name":"Mozambique","code":"MZ"},{"id":"1698","name":"Namibia","code":"NA"},{"id":"1699","name":"New Caledonia","code":"NC"},{"id":"1700","name":"Niger","code":"NE"},{"id":"1701","name":"Norfolk Island","code":"NF"},{"id":"1702","name":"Nicaragua","code":"NI"},{"id":"1703","name":"Nepal","code":"NP"},{"id":"1704","name":"Nauru","code":"NR"},{"id":"1705","name":"Niue","code":"NU"},{"id":"1706","name":"New Zealand","code":"NZ"},{"id":"1707","name":"Oman","code":"OM"},{"id":"1708","name":"Peru","code":"PE"},{"id":"1709","name":"French Polynesia","code":"PF"},{"id":"1710","name":"Papua New Guinea","code":"PG"},{"id":"1711","name":"Pakistan","code":"PK"},{"id":"1712","name":"Saint Pierre and Miquelon","code":"PM"},{"id":"1713","name":"Pitcairn","code":"PN"},{"id":"1714","name":"Puerto Rico","code":"PR"},{"id":"1715","name":"Palestine, State of","code":"PS"},{"id":"1716","name":"Palau","code":"PW"},{"id":"1717","name":"Paraguay","code":"PY"},{"id":"1718","name":"Qatar","code":"QA"},{"id":"1719","name":"Reunion","code":"RE"},{"id":"1720","name":"Serbia","code":"RS"},{"id":"1721","name":"Russian Federation","code":"RU"},{"id":"1722","name":"Rwanda","code":"RW"},{"id":"1723","name":"Solomon Islands","code":"SB"},{"id":"1724","name":"Seychelles","code":"SC"},{"id":"1725","name":"Sudan","code":"SD"},{"id":"1726","name":"Sweden","code":"SE"},{"id":"1727","name":"Saint Helena, Ascension and Tristan da Cunha","code":"SH"},{"id":"1728","name":"Svalbard and Jan Mayen","code":"SJ"},{"id":"1729","name":"Slovakia","code":"SK"},{"id":"1730","name":"Sierra Leone","code":"SL"},{"id":"1731","name":"San Marino","code":"SM"},{"id":"1732","name":"Senegal","code":"SN"},{"id":"1733","name":"Somalia","code":"SO"},{"id":"1734","name":"Suriname","code":"SR"},{"id":"1735","name":"South Sudan","code":"SS"},{"id":"1736","name":"Sao Tome and Principe","code":"ST"},{"id":"1737","name":"El Salvador","code":"SV"},{"id":"1738","name":"Sint Maarten (Dutch part)","code":"SX"},{"id":"1739","name":"Syrian Arab Republic","code":"SY"},{"id":"1740","name":"Swaziland","code":"SZ"},{"id":"1741","name":"Turks and Caicos Islands","code":"TC"},{"id":"1742","name":"Chad","code":"TD"},{"id":"1743","name":"French Southern Territories","code":"TF"},{"id":"1744","name":"Togo","code":"TG"},{"id":"1745","name":"Tajikistan","code":"TJ"},{"id":"1746","name":"Tokelau","code":"TK"},{"id":"1747","name":"Timor-Leste","code":"TL"},{"id":"1748","name":"Turkmenistan","code":"TM"},{"id":"1749","name":"Tunisia","code":"TN"},{"id":"1750","name":"Tonga","code":"TO"},{"id":"1751","name":"Turkey","code":"TR"},{"id":"1752","name":"Trinidad and Tobago","code":"TT"},{"id":"1753","name":"Tuvalu","code":"TV"},{"id":"1754","name":"Taiwan, Province of China","code":"TW"},{"id":"1755","name":"Tanzania, United Republic of","code":"TZ"},{"id":"1756","name":"Ukraine","code":"UA"},{"id":"1757","name":"Uganda","code":"UG"},{"id":"1758","name":"United States Minor Outlying Islands","code":"UM"},{"id":"1759","name":"Uruguay","code":"UY"},{"id":"1760","name":"Uzbekistan","code":"UZ"},{"id":"1761","name":"Holy See (Vatican City State)","code":"VA"},{"id":"1762","name":"Saint Vincent and the Grenadines","code":"VC"},{"id":"1763","name":"Venezuela, Bolivarian Republic of","code":"VE"},{"id":"1764","name":"Virgin Islands, British","code":"VG"},{"id":"1765","name":"Virgin Islands, U.S.","code":"VI"},{"id":"1766","name":"Vanuatu","code":"VU"},{"id":"1767","name":"Wallis and Futuna","code":"WF"},{"id":"1768","name":"Samoa","code":"WS"},{"id":"1769","name":"Yemen","code":"YE"},{"id":"1770","name":"Mayotte","code":"YT"},{"id":"1771","name":"Zambia","code":"ZM"},{"id":"1772","name":"Zimbabwe","code":"ZW"},{"id":"1780","name":"ZZ","code":"ZZ"}]
EEE;
	    $geo = json_decode($geo, true);
	    return array_combine(array_column($geo, "code"), array_column($geo, "name"));
	}
}